#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

names = Set.new %w[
  TASKQUEUE_PORT
  TASKQUEUE_HOST
  REDIS_HOST
  REDIS_PORT
]

defaults = relevant_defaults(names)
env = docker_env(defaults)

service_port = defaults['TASKQUEUE_PORT'] || '3060'
service_host = defaults['TASKQUEUE_HOST'] || '172.17.0.1'
service_name = 'taskqueue'
service_image = "#{service_name}-alpine:v0.001-#{service_port}"

docker_rm "#{service_name}-#{service_port}"

bind_list = []
[service_host, '172.17.0.1'].uniq.each do |ip|
  bind_list << '-p'
  bind_list << "#{ip}:#{service_port}:#{service_port}"
end

cmd = %W[
  docker run
  --name #{service_name}-#{service_port}
  --restart=always
  -d
  -u 1090:1090
]
cmd += bind_list
cmd += env
cmd += %W[
  -v /etc/localtime:/etc/localtime:ro
  #{service_image}
]

cmd += ['sh', '-c', "umask 002 && ./#{service_name}"]

system(*cmd)
